#include "UpdateTimer.h"

UpdateTimer::UpdateTimer( void ) : IKernelTask( "UpdateTimer", 20 )
{
	ulLastTick = 0;
	ulCurrentTick = 0;
	ulStartTick = 0;
	fTimeDelta = 0;
	logger << "UpdateTimer constructed" << std::endl;
}

UpdateTimer::~UpdateTimer()
{
	logger << "UpdateTimer destructed" << std::endl;
}

GCPtr<UpdateTimer> UpdateTimer::getInstance()
{
	static GCPtr<UpdateTimer> timer = new UpdateTimer();
	return timer;
}

bool UpdateTimer::Start()
{
	ulStartTick = SDL_GetTicks();
	ulCurrentTick = ulStartTick;
	ulLastTick = ulStartTick;

	logger << "Task started. Current timestamp: " << ulCurrentTick << std::endl;

	return true;
}

void UpdateTimer::Stop()
{
	ulLastTick = 0;
	ulCurrentTick = 0;
	ulStartTick = 0;
	fTimeDelta = 0;
	logger << "UpdateTimer stopped" << std::endl;
}

void UpdateTimer::Update()
{
	ulLastTick = ulCurrentTick;
	ulCurrentTick = SDL_GetTicks();
	fTimeDelta = static_cast<float>( ( ulCurrentTick - ulLastTick ) );

	//logger << INFO << "Task updated. Time since last update: " << fTimeDelta << "ms" << std::endl;

	if( fTimeDelta > 50 ) // Depends on the game
		fTimeDelta = 50;
}

float UpdateTimer::getTimeDelta()
{
	return fTimeDelta;
}

float UpdateTimer::getTotalTime()
{
	return static_cast<float>( ( ulCurrentTick - ulStartTick ) );
}